home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / src / mailcheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-12  |  10.5 KB  |  415 lines

  1. /* mailcheck.c -- The check is in the mail... */
  2.  
  3. /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include "posixstat.h"
  24. #include <sys/param.h>
  25. #include "shell.h"
  26.  
  27. /**
  28.  ** (sjk)++ Remove all host/mail references on the Atari ST1
  29.  **/
  30. #if !defined(atarist)
  31.  
  32. #ifndef MAXPATHLEN
  33. #define MAXPATHLEN 1024
  34. #endif
  35.  
  36. #ifndef NOW
  37. #define NOW ((time_t)time ((time_t *)0))
  38. #endif
  39.  
  40. typedef struct {
  41.   char *name;
  42.   time_t access_time;
  43.   time_t mod_time;
  44.   long file_size;
  45. } FILEINFO;
  46.  
  47. /* The list of remembered mail files. */
  48. FILEINFO **mailfiles = (FILEINFO **)NULL;
  49.  
  50. /* Number of mail files that we have. */
  51. int mailfiles_count = 0;
  52.  
  53. /* The last known time that mail was checked. */
  54. int last_time_mail_checked = 0;
  55.  
  56. /* Returns non-zero if it is time to check mail. */
  57. time_to_check_mail ()
  58. {
  59.   char *temp = get_string_value ("MAILCHECK");
  60.   time_t now = NOW;
  61.   unsigned seconds = 0;
  62.  
  63.   if ((!temp || sscanf (temp, "%u", &seconds) == 0) ||
  64.       ((now - last_time_mail_checked < seconds)))
  65.     return (0);
  66.  
  67.   return (1);
  68. }
  69.  
  70. /* Okay, we have checked the mail.  Perhaps I should make this function
  71.    go away. */
  72. reset_mail_timer ()
  73. {
  74.   last_time_mail_checked = NOW;
  75. }
  76.  
  77. /* Locate a file in the list.  Return index of
  78.    entry, or -1 if not found. */
  79. find_mail_file (file)
  80.      char *file;
  81. {
  82.   register int index = 0;
  83.  
  84.   while (index < mailfiles_count)
  85.     if (strcmp ((mailfiles[index])->name, file) == 0) return index;
  86.     else index++;
  87.   return (-1);
  88. }
  89.  
  90. /* Add this file to the list of remembered files. */
  91. add_mail_file (file)
  92.      char *file;
  93. {
  94.   struct stat finfo;
  95.   char *full_pathname ();
  96.   char *filename = full_pathname (file);
  97.   int index = find_mail_file (file);
  98.  
  99.   if (index > -1)
  100.     {
  101.       if (stat (filename, &finfo) == 0)
  102.     {
  103.       mailfiles[index]->mod_time = finfo.st_mtime;
  104.       mailfiles[index]->access_time = finfo.st_atime;
  105.       mailfiles[index]->file_size = (long)finfo.st_size;
  106.     }
  107.       free (filename);
  108.       return;
  109.     }
  110.  
  111.   mailfiles = (FILEINFO **)
  112.     xrealloc (mailfiles,
  113.           ((++mailfiles_count) * sizeof (FILEINFO *)));
  114.  
  115.   mailfiles[mailfiles_count - 1] = (FILEINFO *)xmalloc (sizeof (FILEINFO));
  116.   mailfiles[mailfiles_count - 1]->name = filename;
  117.   if (stat (filename, &finfo) == 0)
  118.     {
  119.       mailfiles[mailfiles_count - 1]->access_time = finfo.st_atime;
  120.       mailfiles[mailfiles_count - 1]->mod_time = finfo.st_mtime;
  121.       mailfiles[mailfiles_count - 1]->file_size = finfo.st_size;
  122.     }
  123.   else
  124.     {
  125.       mailfiles[mailfiles_count - 1]->access_time
  126.     = mailfiles[mailfiles_count - 1]->mod_time = (time_t)-1;
  127.       mailfiles[mailfiles_count - 1]->file_size = (long)-1;
  128.     }
  129. }
  130.  
  131. /* Reset the existing mail files access and modification times to zero. */
  132. reset_mail_files ()
  133. {
  134.   register int i;
  135.  
  136.   for (i = 0; i < mailfiles_count; i++)
  137.     {
  138.       mailfiles[i]->access_time = mailfiles[i]->mod_time = 0;
  139.       mailfiles[i]->file_size = (long)0;
  140.     }
  141. }
  142.  
  143. /* Free the information that we have about the remembered mail files. */
  144. free_mail_files ()
  145. {
  146.   while (mailfiles_count--)
  147.     {
  148.       free (mailfiles[mailfiles_count]->name);
  149.       free (mailfiles[mailfiles_count]);
  150.     }
  151.  
  152.   if (mailfiles)
  153.     free (mailfiles);
  154.  
  155.   mailfiles_count = 0;
  156.   mailfiles = (FILEINFO **)NULL;
  157. }
  158. #endif 
  159.  
  160. /* Return the full pathname of FILE.  Easy.  Filenames that begin
  161.    with a '/' are returned as themselves.  Other filenames have
  162.    the current working directory prepended.  A new string is
  163.    returned in either case. */
  164. char *
  165. full_pathname (file)
  166.      char *file;
  167. {
  168.   char *tilde_expand (), *disposer;
  169.  
  170.   if (*file == '~')
  171.     file = tilde_expand (file);
  172.   else
  173.     file = savestring (file);
  174.  
  175.   if (absolute_pathname (file))
  176.     if (*file == '/')
  177.       return (file);
  178.  
  179.   disposer = file;
  180.  
  181.   {
  182.     char *current_dir = (char *)xmalloc (2 + MAXPATHLEN + strlen (file));
  183.     if (!getwd (current_dir))
  184.       {
  185.     report_error (current_dir);
  186.     free (current_dir);
  187.     return ((char *)NULL);
  188.       }
  189.     strcat (current_dir, "/");
  190.  
  191.     /* Turn /foo/./bar into /foo/bar. */
  192.     if (strncmp (file, "./", 2) == 0)
  193.       file += 2;
  194.  
  195.     strcat (current_dir, file);
  196.     free (disposer);
  197.     return (current_dir);
  198.   }
  199. }
  200.  
  201. /**
  202.  ** (sjk)++ Remove all host/mail references on the Atari ST
  203.  **/
  204. #if !defined(atarist)
  205.  
  206. /* Return non-zero if FILE's mod date has changed. */
  207. file_mod_date_changed (file)
  208.      char *file;
  209. {
  210.   time_t time = (time_t)NULL;
  211.   struct stat finfo;
  212.  
  213.   int index = find_mail_file (file);
  214.   if (index != -1)
  215.     time = mailfiles[index]->mod_time;
  216.  
  217.   if (stat (file, &finfo) == 0)
  218.     {
  219.       if (finfo.st_size != 0)
  220.     return (time != finfo.st_mtime);
  221.     }
  222.   return (0);
  223. }
  224.  
  225. /* Return non-zero if FILE's access date has changed. */
  226. file_access_date_changed (file)
  227.      char *file;
  228. {
  229.   time_t time = (time_t)NULL;
  230.   struct stat finfo;
  231.  
  232.   int index = find_mail_file (file);
  233.   if (index != -1)
  234.     time = mailfiles[index]->access_time;
  235.  
  236.   if (stat (file, &finfo) == 0)
  237.     {
  238.       if (finfo.st_size != 0)
  239.     return (time != finfo.st_atime);
  240.     }
  241.   return (0);
  242. }
  243.  
  244. /* Return non-zero if FILE's size has increased. */
  245. file_has_grown (file)
  246.      char *file;
  247. {
  248.   long size = 0L;
  249.   struct stat finfo;
  250.  
  251.   int i = find_mail_file (file);
  252.   if (i != -1)
  253.     size = mailfiles[i]->file_size;
  254.  
  255.   if (stat (file, &finfo) == 0)
  256.     {
  257.       return (finfo.st_size > size);
  258.     }
  259.   return (0);
  260. }
  261.  
  262. #if defined (USG)
  263. #define DEFAULT_MAIL_PATH "/usr/mail/"
  264. #else
  265. #define DEFAULT_MAIL_PATH "/usr/spool/mail/"
  266. #endif
  267.  
  268. /* Return the colon separated list of pathnames to check for mail. */
  269. char *
  270. get_mailpaths ()
  271. {
  272.   extern char *current_user_name;
  273.   char *mailpaths = get_string_value ("MAILPATH");
  274.   if (!mailpaths) mailpaths = get_string_value ("MAIL");
  275.   if (!mailpaths)
  276.     {
  277.       mailpaths = (char *)alloca (1 + strlen (DEFAULT_MAIL_PATH)
  278.                   + strlen (current_user_name));
  279.       strcpy (mailpaths, DEFAULT_MAIL_PATH);
  280.       strcat (mailpaths, current_user_name);
  281.     }
  282.   return (savestring (mailpaths));
  283. }
  284.  
  285. /* Remember the dates of the files specified by MAILPATH, or if there is
  286.    no MAILPATH, by the file specified in MAIL.  If neither exists, use a
  287.    default value, which we randomly concoct from using Unix. */
  288. remember_mail_dates ()
  289. {
  290.   char *mailpaths = get_mailpaths ();
  291.   char *mailfile, *extract_colon_unit ();
  292.   int index = 0;
  293.   
  294.   while (mailfile = extract_colon_unit (mailpaths, &index))
  295.     {
  296.       register int i;
  297.       for (i = 0;
  298.        mailfile[i] && mailfile[i] != '?' && mailfile[i] != '%';
  299.        i++);
  300.       mailfile[i] = '\0';
  301.       add_mail_file (mailfile);
  302.     }
  303. }
  304.  
  305. /* check_mail () is useful for more than just checking mail.  Since it has
  306.    the paranoids dream ability of telling you when someone has read your
  307.    mail, it can just as easily be used to tell you when someones .profile
  308.    file has been read, thus letting one know when someone else has logged
  309.    in.  Pretty good, huh? */
  310.  
  311. /* Check for mail in some files.  If the modification date of any
  312.    of the files in MAILPATH has changed since we last did a
  313.    remember_mail_dates () then mention that the user has mail.
  314.    Special hack:  If the shell variable MAIL_WARNING is on and the
  315.    mail file has been accessed since the last time we remembered, then
  316.    the message "The mail in <mailfile> has been read" is printed. */
  317. check_mail ()
  318. {
  319.   register int string_index;
  320.   char *extract_colon_unit ();
  321.   char *current_mail_file, *you_have_mail_message;
  322.   char *mailpaths = get_mailpaths ();
  323.   int index = 0;
  324.   char *dollar_underscore = get_string_value ("_");
  325.  
  326.   while ((current_mail_file = extract_colon_unit (mailpaths, &index)))
  327.     {
  328.       char *t;
  329.       int use_user_notification;
  330.  
  331.       if (!*current_mail_file)
  332.     {
  333.       free (current_mail_file);
  334.       continue;
  335.     }
  336.  
  337.       t = full_pathname (current_mail_file);
  338.       free (current_mail_file);
  339.       current_mail_file = t;
  340.  
  341.       use_user_notification = 0;
  342.       you_have_mail_message = "You have mail in $_";
  343.  
  344.       for (string_index = 0; current_mail_file[string_index]; string_index++)
  345.     if (current_mail_file[string_index] == '?'
  346.         || current_mail_file[string_index] == '%')
  347.       {
  348.         current_mail_file[string_index] = '\0';
  349.         you_have_mail_message = current_mail_file + string_index + 1;
  350.         use_user_notification++;
  351.         break;
  352.       }
  353.  
  354.       if (file_mod_date_changed (current_mail_file))
  355.     {
  356.       WORD_LIST *tlist, *expand_string ();
  357.       char *string_list ();
  358.       int i, file_is_bigger;
  359.       bind_variable ("_", current_mail_file);
  360.  
  361.       /* Have to compute this before the call to add_mail_file, which
  362.          resets all the information. */
  363.       file_is_bigger = file_has_grown (current_mail_file);
  364.  
  365.       add_mail_file (current_mail_file);
  366.  
  367.       i = find_mail_file (current_mail_file);
  368.  
  369.       /* If the user has just run a program which manipulates the
  370.          mail file, then don't bother explaining that the mail
  371.          file has been manipulated.  Since some systems don't change
  372.          the access time to be equal to the modification time when
  373.          the mail in the file is manipulated, check the size also.  If
  374.          the file has not grown, continue. */
  375.       if (i != -1 &&
  376.           (mailfiles[i]->access_time == mailfiles[i]->mod_time) &&
  377.           !file_is_bigger)
  378.         goto next_mail_file;
  379.  
  380.       if (!use_user_notification)
  381.         {
  382.           if (i != -1 &&
  383.           (mailfiles[i]->access_time < mailfiles[i]->mod_time) &&
  384.           file_is_bigger)
  385.         you_have_mail_message = "You have new mail in $_";
  386.         }
  387.  
  388.       if ((tlist = expand_string (you_have_mail_message, 1)))
  389.         {
  390.           char *tem = string_list (tlist);
  391.           you_have_mail_message = (char *)alloca (1 + strlen (tem));
  392.           strcpy (you_have_mail_message, tem);
  393.           free (tem);
  394.         }
  395.       else
  396.         you_have_mail_message = "";
  397.  
  398.       printf ("%s\n", you_have_mail_message);
  399.       dispose_words (tlist);
  400.     }
  401.  
  402.       if (find_variable ("MAIL_WARNING")
  403.       && file_access_date_changed (current_mail_file))
  404.     {
  405.       add_mail_file (current_mail_file);
  406.       printf ("The mail in %s has been read!\n", current_mail_file);
  407.     }
  408.     next_mail_file: 
  409.       free (current_mail_file);
  410.     }
  411.   free (mailpaths);
  412.   bind_variable ("_", dollar_underscore);
  413. }
  414. #endif 
  415.